import {TBizcEneryEquipApi} from '@/api/equip/tBizcEneryEquipApi.js'
import {warehouseManagement} from '@/api/equip/warehouseManagement.js'
import {accountQueryApi} from '@/api/equip/accountQueryApi.js'
import {countList} from '@/api/equip/countList.js'
import {Message, MessageBox} from 'element-ui';
import util from '@/utils/util'
import {getInfo} from "@/api/login";
import {listDept} from "@/api/system/dept";

export const GridEvent = {
  OptionData_yxzt: [],
  OptionData_sblxmc: [],
  OptionData_djhszt: [],
  newForm() {  //打开新增页面窗口//
    formModel.title = '新增';
    formModel.dialogVisible = true;
    formModel.width = '70%';
    formModel.componentId = 'TBizcEneryEquipForm';
    formModel.TBizcEneryEquipFormData.type = 'insert';
  },
  editForm(row) { 				//数据编辑//
    formModel.title = '设备详情';
    formModel.dialogVisible = true;
    formModel.width = '70%';
    formModel.componentId = 'TBizcEneryEquipForm';
    formModel.TBizcEneryEquipFormData.type = 'update';
    formModel.TBizcEneryEquipFormData.data = row;
    console.log(formModel.TBizcEneryEquipFormData.data)
  },
  // exportData:{
  //   columnCodeList:[],
  //   columnNameList:[],
  //   columnWidthList:[]
  // },
  getExportInfo(xGrid) {

  },
  gldwbmChangeEvent: (xGrid, value) => {
    xGrid._data.formData.warehouseNo = null
    warehouseManagement.getKfByGldw(value.data.deptNo).then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '请选择', value: ''}
        ]
        res.data.forEach((item) => {
          // console.log(item,'item.,,,');
          optionList.push({label: item.kfName, value: item.id})
        })
        const item = xGrid.getFormItems(5)
        item.itemRender.options = optionList
      }
    })
  },
  getOptionData_gldw: (xGrid) => {//获取管理单位：字典表
    TBizcEneryEquipApi.queryDictData('EN_EQUIP_GLDW').then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '请选择', value: ''}
        ]
        res.data.forEach((item) => {
          // console.log(item,'item.,,,');
          optionList.push({label: item.value, value: item.code})
        })
        const item = xGrid.getFormItems(1)
        item.itemRender.options = optionList
        GridEvent.OptionData_gldw = optionList;
      }
    }).catch(err => {
      console.log(err)
    })
  },
  getOptionData_sblx: (xGrid) => {
    accountQueryApi.queryDictDatasb('1000').then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '请选择', value: ''}
        ]
        res.data.forEach((item) => {
          optionList.push({
            label: item.name, value: item.id
          })
        })

        const item = xGrid.getFormItems(3)
        item.itemRender.options = optionList
        GridEvent.OptionData_sblxmc = optionList;
      }
    }).catch(err => {
      console.log(err)
    })
  },

  getOptionData_dw: (xGrid) => {
    getInfo().then(res => {
      let params = {
        deptId: res.user.dept.deptId,
        parentId: res.user.dept.parentId
      }
      listDept(params).then(result => {
        let optionList = [
          {label: '请选择', value: ''}
        ]
        result.data.forEach((item) => {
          optionList.push({label: item.deptName, value: item.deptId})

        })
        const item = xGrid.getFormItems(0)
        item.itemRender.options = optionList
        GridEvent.OptionData_gldw = optionList;
      })

    })
  },
  getOptionData_pdfs: (xGrid) => {
    TBizcEneryEquipApi.queryDictData('EN_STOCK_CHECK_TYPE').then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '请选择', value: ''}
        ]
        res.data.forEach((item) => {
          optionList.push({label: item.value, value: item.code})
        })
        const item = xGrid.getFormItems(4)
        item.itemRender.options = optionList
      }
    }).catch(err => {
      console.log(err)
    })
  },
  getOptionData_warehouseNo: (xGrid) => {//获取运行状态：字典表
    warehouseManagement.getKfList().then(res => {
      if (xGrid) {
        // 修改编辑框
        // const sexColumn = $grid.getColumnByField('sex')
        // sexColumn.editRender.options = sexList
        // 修改查询条件列表
        let optionList = [
          {label: '请选择', value: ''}
        ]
        res.data.forEach((item) => {
          optionList.push({label: item.kfName, value: item.id})
        })
        const item = xGrid.getFormItems(5)
        item.itemRender.options = optionList
      }
    }).catch(err => {
      console.log(err)
    })
  },

  importXls(info) {   //模板导入
    let formData = new FormData();
    formData.append("file", info.file);
    TBizcEneryEquipApi.excelImport(formData).then(res => {
      this.$message.success(res)
      // console.log(res,'sssss');
    }).catch(err => {
      console.log(err)
    })
  },
  uploadFile(info) {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      this.$message.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      this.$message.error(`${info.file.name} file upload failed.`);
    }
  },

  exportXls(xGrid) {//导出
    if (xGrid) {
      const dataParams = {}

      const queryParams = Object.assign({}, xGrid.getProxyInfo().form)
      const pageParams = {
        pageSize: xGrid.tablePage.pageSize,
        pageNo: xGrid.tablePage.currentPage,
        entity: queryParams,
        column: "",
        order: "",
        paramsMap: {}
      }
      countList.exportXls(pageParams).then(res => {
        util.download(res, '盘点详情列表' + (moment().format("YYYY-MM-DD")) + '.xls')
      }).catch(err => {
        console.log(err)
      })


      // GridEvent.getExportInfo(xGrid);
      // let newColumnCodeList=[];
      // let newColumnNameList=[];
      // let newColumnWidthList=[];
      // let columnCodeList=[];
      // let columnNameList=[];
      // let columnWidthList=[];
      // let columns = xGrid.columns;

      // columns.forEach(item=>{
      //   if( item.field ){
      //     if(item.hasOwnProperty('visible')){

      //       if(item.visible){ //显示的状态
      //         newColumnCodeList.push(item.field)
      //         newColumnNameList.push(item.title)
      //         newColumnWidthList.push(item.width?item.width:200)
      //       }
      //     }else{
      //       columnCodeList.push(item.field)
      //       columnNameList.push(item.title)
      //       columnWidthList.push(item.width?item.width:200)
      //     }
      //   }
      // })
      // dataParams['columnCodeList'] =columnCodeList
      // dataParams['columnNameList'] =columnNameList
      // dataParams['columnWidthList'] =columnWidthList
      //   // dataParams['columnCodeList'] =GridEvent.getExportInfo.columnCodeList.join(",");
      //   // dataParams['columnNameList'] =GridEvent.getExportInfo.columnNameList.join(",");
      //   // dataParams['columnWidthList'] =GridEvent.getExportInfo.columnWidthList.join(",");
      //   dataParams['fileName'] = '盘点列表详情';
      //   dataParams['dataNum'] = xGrid.tablePage.total;
      //   // queryParams['pageNo'] = xGrid.tablePage.currentPage;
      //   // queryParams['pageSize'] = xGrid.tablePage.pageSize;
      //   dataParams['url'] = '/t-en-stock-task/export/excel';
      //   dataParams['queryParams'] = pageParams
      //   TBizcEneryEquipApi.exportXls(dataParams).then(res=>{
      //     GridEvent.download(res,'盘点列表详情'+'.xls')
      //   }).catch(err=>{
      //     console.log(err)
      //   })
    }
  },

  download(data, fileName) {  //下载核心方法
    if (!data) {
      return
    }
    let blob = new Blob([data])
    // debugger
    if ('download' in document.createElement('a')) { // 不是IE浏览器
      let url = window.URL.createObjectURL(blob)
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link) // 下载完成移除元素
      window.URL.revokeObjectURL(url) // 释放掉blob对象
    } else { // IE 10+
      window.navigator.msSaveBlob(blob, fileName)
    }
  },
  deleteEvent(rows, xGrid) {  	//数据删除//
    MessageBox.confirm('此操作将永久删除, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      // const queryParams = Object.assign({}, rows)
      // queryParams[0].sfsc = '1'
      let queryParams = [{
        sbbm: rows[0].sbbm,
        sfsc: '1'
      }]
      // console.log(rows,'rows');
      //删除数据后台如何处理，按照实际的�? 处理多行记录信息rows//
      TBizcEneryEquipApi.update(queryParams).then(res => {
        //删除成功后，会掉表格刷新//
        xGrid.commitProxy('query');
        Message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(err => {
        console.error(err)
      })
    }).catch(() => {
      Message({
        type: 'info',
        message: '已取消删!'
      });
    });
  },
  initFormData(formData) { 		//查询框数据初始化//
                              // console.log(formData,'99999',GridEvent.OptionData_yxzt);
                              // formData.yxzt = GridEvent.OptionData_yxzt
  },
  queryData(page, formData) {  //查询表格数据//
    const queryParams = Object.assign({}, formData)
    const pageParams = {
      pageSize: page.pageSize,
      pageNo: page.currentPage,
      entity: queryParams,
      paramsMap: {},
      column: '',
      order: ''
    };
    return countList.getdata(pageParams)
  },


}
export const formModel = {
  title: '',            //窗口标题//
  dialogVisible: false, //是否弹出显示//
  width: '70%',          //窗口宽度//
  fullscreen: false,    //是否全屏//
  closeOnClickModal: false, //是否可以通过点击 modal 关闭 Dialog//
  top: '5vh',			 //顶部位置//
  componentId: 'TBizcEneryEquipForm',  //默认显示组件//

  TBizcEneryEquipFormData: {
    type: '',
    data: {
      deptName: '',//单位
      wzlbmc: '三省物资',//物资类别
      sblxmc: '',        //设备类型名称//
      sblx: '',        //设备类型编码  //
      sbbm: '',        //设备编码//
      sbmc: '',        //设备名称//
      factoryNo: '',        //出厂编码//
      sskfmc: '',//所属库房
      sbgg: '',        //规格//
      xh: '',        //型号//
      jlfsmc: '',        //计量方式//
      yxzt: '',        //运行状态  10-未投运
      pt: '',
      ct: '',
      yxztmc: '',//运行状态
      cjmc: '',        //厂家名称//
      ifTest: '',//是否检测
      sftq: '',//是否贴钱
      receivingCoordinate: '',//坐标
      sbdz: '',//地址
      ccrq: '',        //出场日期//
      createTime: '',//创建日期
      creator: '',//创建人
    }
  },
  closeDialogEvent(reload, xGrid) {
    formModel.dialogVisible = false;
    if (reload) {
      xGrid.commitProxy('query');
    }
  }
}

